<template>
  <div>
    <div class="provider-list">
      <div class="header">
        <div class="header-line2 flex-s">
          <el-col :span="6.6">
            <el-date-picker
              type="daterange"
              range-separator="To"
              v-model="state.chooseTime"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              size="large"
              prefix-icon="Calendar"
              value-format="YYYY-MM-DD"
              @change="changeTime"
            />
          </el-col>
          <el-select
            width="150"
            show-border
            class="ml20"
            v-model="state.params.tiaozhuanleixing"
            size="large"
            placeholder="跳转类型"
            clearable
            @change="GetList"
          >
            <el-option
              v-for="item in state.type"
              :key="item.dicValue"
              :label="item.dicLabel"
              :value="item.dicValue"
            >
              {{ item.dicLabel }}
            </el-option>
          </el-select>
          <el-select
            width="150"
            show-border
            class="ml20"
            v-model="state.params.fuwuguishu"
            size="large"
            placeholder="类型"
            clearable
            @change="GetList"
          >
            <el-option
              v-for="item in state.option"
              :key="item.dicValue"
              :label="item.dicLabel"
              :value="item.dicValue"
            >
              {{ item.dicLabel }}
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="content content-table">
        <el-table
          ref="multipleTableRef"
          border
          :data="state.list"
          style="width: calc(100% - 20px)"
        >
          <el-table-column
            property="mingcheng"
            label="名称"
            width="100"
            align="center"
          />
          <el-table-column property="biaoti" label="图片" align="center">
            <template #default="scope">
              <img
                v-if="scope.row.tubiao && scope.row.tubiao.length > 0"
                style="width: 100px; height: 100px; border-radius: 50%"
                :src="scope.row.tubiao[0].filePath"
                alt=""
              />
            </template>
          </el-table-column>
          <el-table-column
            property="fuwuguishu"
            label="服务归属"
            align="center"
          />
          <el-table-column
            property="tiaozhuanleixing"
            label="跳转类型"
            align="center"
          />
          <el-table-column
            property="fuwujieshao"
            label="服务介绍"
            width="300"
            align="center"
          />
          <el-table-column
            property="updateTime"
            label="修改时间"
            width="300"
            align="center"
          />
          <el-table-column label="状态" align="center">
            <template #default="scope">
              <span v-if="scope.row.shangjia">{{
                scope.row.shangjia.code == 0 ? "已下架" : "已上架"
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="200"
            align="center"
          >
            <template #default="scope">
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'edit')"
                >编辑</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="changeStatus(scope.row)"
                >{{
                  scope.row.shangjia && scope.row.shangjia.code == 0
                    ? "上架"
                    : "下架"
                }}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { GetServeList, ChangeServeStatus } from "@/api/serve";
import { GetDictionaryTree } from "@/api/system";
import { baseMsg, elMessageTip } from "@/utils/utils";
const route = useRoute();
const router = useRouter();
const state = reactive({
  params: {
    current: Number(route.query.page) || 1,
    size: Number(route.query.size) || 10,
    fuwuguishu: "",
    tiaozhuanleixing: "",
    kaishiriqi: "",
    jieshuriqi: "",
  },
  list: [],
  option: [],
  type: [],
  // 时间选择器时间
  chooseTime: [],
  total: 0,
});
// 服务跳转类型和服务归属下拉查询
const GetType = () => {
  GetDictionaryTree({ dicModel: "fuwuguishu" }).then((res) => {
    if (res.code == 4201) {
      state.option = res.data;
    }
  });
  GetDictionaryTree({ dicModel: "fuwuguanlitiaozhuanleixing" }).then((res) => {
    if (res.code == 4201) {
      state.type = res.data;
    }
  });
};
const GetList = () => {
  GetServeList(state.params).then((res) => {
    if (res.code == 4201) {
      state.list = res.data;
    }
  });
};
// 时间选择
const changeTime = (value) => {
  if (value) {
    state.params.kaishiriqi = value[0];
    state.params.jieshuriqi = value[1];
  } else {
    state.params.kaishiriqi = "";
    state.params.jieshuriqi = "";
  }
  GetList();
};
// 操作
const onOperation = (params, operation) => {
  if (operation === "edit") {
    router.push({
      path: `/fuwu/info`,
      query: {
        id: params.id,
        type: operation,
      },
    });
  }
};
// 上下架
const changeStatus = async (value) => {
  const res = await ChangeServeStatus({ id: value.id });
  if (res.code === 4202) {
    baseMsg("操作成功");
    GetList();
  }
};
watch(
  () => route.query,
  () => {
    state.params.current = Number(route.query.page) || 1;
    GetList();
  },
  { deep: true }
);
GetList();
GetType();
</script>
<style lang="scss" scoped>
.header {
  border-bottom: 10px solid #f2f2f2;
  border-radius: 10px 10px 0 0;
}
.header-line1 {
  padding: 20px 10px;
  border-bottom: 1px solid #ccc;
  border-radius: 10px 10px 0 0;
}
.header-line2 {
  padding: 20px 10px;
}
.content {
  border: none !important;
  margin: 20px 10px;
  background-color: transparent !important;
  padding-bottom: 30px;
}
</style>
<style lang="scss">
.content-table .el-table__header th {
  background-color: #f2f9fc !important;
}
</style>
